/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.businessgoldstream.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.anny.businessgoldstream.entity.AnnyBusinessgoldStream;
import com.jeeplus.modules.anny.businessgoldstream.service.AnnyBusinessgoldStreamService;

/**
 * 创业金流水表Controller
 * @author linzx
 * @version 2018-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/anny/businessgoldstream/annyBusinessgoldStream")
public class AnnyBusinessgoldStreamController extends BaseController {

	@Autowired
	private AnnyBusinessgoldStreamService annyBusinessgoldStreamService;
	
	@ModelAttribute
	public AnnyBusinessgoldStream get(@RequestParam(required=false) String id) {
		AnnyBusinessgoldStream entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyBusinessgoldStreamService.get(id);
		}
		if (entity == null){
			entity = new AnnyBusinessgoldStream();
		}
		return entity;
	}

	/**
	 * 添加创业金流水
	 * @param accountId
	 * @param amout
	 * @param incomeFlag
	 * @param source
	 */
	@RequestMapping(value = "saveBusinessgoldStream")
	public void saveBusinessgoldStream(String accountId,String amout,String incomeFlag,String source){
		AnnyBusinessgoldStream annyBusinessgoldStream =new AnnyBusinessgoldStream();
		annyBusinessgoldStream.setAccountId(accountId);
		annyBusinessgoldStream.setAmout(amout);
		annyBusinessgoldStream.setIncomeFlag(incomeFlag);
		annyBusinessgoldStream.setSource(source);
		annyBusinessgoldStreamService.save(annyBusinessgoldStream);
	}

	/**
	 * 通过用户id查询创业金流水
	 * @param accountId
	 * @return
	 */
	@RequestMapping(value = "selectAccountId")
	public List<AnnyBusinessgoldStream> selectAccountId(String accountId){
	List<AnnyBusinessgoldStream> annyBusinessgoldStreams =	annyBusinessgoldStreamService.selectAccountId(accountId);
	return annyBusinessgoldStreams;
	}
	/**
	 * 创业金列表页面
	 */
	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/anny/businessgoldstream/annyBusinessgoldStreamList";
	}

		/**
	 * 创业金列表数据
	 */
	@ResponseBody
	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyBusinessgoldStream annyBusinessgoldStream, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnyBusinessgoldStream> page = annyBusinessgoldStreamService.findPage(new Page<AnnyBusinessgoldStream>(request, response), annyBusinessgoldStream); 
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑创业金表单页面
	 */
	@RequiresPermissions(value={"anny:businessgoldstream:annyBusinessgoldStream:view","anny:businessgoldstream:annyBusinessgoldStream:add","anny:businessgoldstream:annyBusinessgoldStream:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyBusinessgoldStream annyBusinessgoldStream, Model model) {
		model.addAttribute("annyBusinessgoldStream", annyBusinessgoldStream);
		if(StringUtils.isBlank(annyBusinessgoldStream.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/businessgoldstream/annyBusinessgoldStreamForm";
	}

	/**
	 * 保存创业金
	 */
	@RequiresPermissions(value={"anny:businessgoldstream:annyBusinessgoldStream:add","anny:businessgoldstream:annyBusinessgoldStream:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyBusinessgoldStream annyBusinessgoldStream, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyBusinessgoldStream)){
			return form(annyBusinessgoldStream, model);
		}
		//新增或编辑表单保存
		annyBusinessgoldStreamService.save(annyBusinessgoldStream);//保存
		addMessage(redirectAttributes, "保存创业金成功");
		return "redirect:"+Global.getAdminPath()+"/anny/businessgoldstream/annyBusinessgoldStream/?repage";
	}
	
	/**
	 * 删除创业金
	 */
	@ResponseBody
	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyBusinessgoldStream annyBusinessgoldStream, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyBusinessgoldStreamService.delete(annyBusinessgoldStream);
		j.setMsg("删除创业金成功");
		return j;
	}
	
	/**
	 * 批量删除创业金
	 */
	@ResponseBody
	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyBusinessgoldStreamService.delete(annyBusinessgoldStreamService.get(id));
		}
		j.setMsg("删除创业金成功");
		return j;
	}
	
//	/**
//	 * 导出excel文件
//	 */
//	@ResponseBody
//	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:export")
//    @RequestMapping(value = "export", method=RequestMethod.POST)
//    public AjaxJson exportFile(AnnyBusinessgoldStream annyBusinessgoldStream, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		AjaxJson j = new AjaxJson();
//		try {
//            String fileName = "创业金"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//            Page<AnnyBusinessgoldStream> page = annyBusinessgoldStreamService.findPage(new Page<AnnyBusinessgoldStream>(request, response, -1), annyBusinessgoldStream);
//    		new ExportExcel("创业金", AnnyBusinessgoldStream.class).setDataList(page.getList()).write(response, fileName).dispose();
//    		j.setSuccess(true);
//    		j.setMsg("导出成功！");
//    		return j;
//		} catch (Exception e) {
//			j.setSuccess(false);
//			j.setMsg("导出创业金记录失败！失败信息："+e.getMessage());
//		}
//			return j;
//    }
//
//	/**
//	 * 导入Excel数据
//
//	 */
//	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:import")
//    @RequestMapping(value = "import", method=RequestMethod.POST)
//    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
//		try {
//			int successNum = 0;
//			int failureNum = 0;
//			StringBuilder failureMsg = new StringBuilder();
//			ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<AnnyBusinessgoldStream> list = ei.getDataList(AnnyBusinessgoldStream.class);
//			for (AnnyBusinessgoldStream annyBusinessgoldStream : list){
//				try{
//					annyBusinessgoldStreamService.save(annyBusinessgoldStream);
//					successNum++;
//				}catch(ConstraintViolationException ex){
//					failureNum++;
//				}catch (Exception ex) {
//					failureNum++;
//				}
//			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条创业金记录。");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条创业金记录"+failureMsg);
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入创业金失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/businessgoldstream/annyBusinessgoldStream/?repage";
//    }
//
//	/**
//	 * 下载导入创业金数据模板
//	 */
//	@RequiresPermissions("anny:businessgoldstream:annyBusinessgoldStream:import")
//    @RequestMapping(value = "import/template")
//    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		try {
//            String fileName = "创业金数据导入模板.xlsx";
//    		List<AnnyBusinessgoldStream> list = Lists.newArrayList();
//    		new ExportExcel("创业金数据", AnnyBusinessgoldStream.class, 1).setDataList(list).write(response, fileName).dispose();
//    		return null;
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/businessgoldstream/annyBusinessgoldStream/?repage";
//    }

}